﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using ST.Buz.Base.Buz_ViewModel;
using ST.Buz.Base.Entity;
using ST.Buz.Base.Enums;
using ST.Buz.Base.ViewModel;
using ST.Buz.Service;
using ST.Common.Util;
using ST.Common.Util.Model;
using ST.Common.Util.Mvc;
using ST.Common.Util.Text;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;

namespace ST.Buz.Web.Controllers
{
	[Route("machining")]
	public class MachiningController : MyBaseController
	{
		#region Ctor
		private readonly IServiceMachining serviceMachining;
		private readonly IServiceFinanceRecvDue serviceFinanceRecvDue;
		private readonly IServiceSysEnumCode serviceSysEnumCode;
		private readonly IServiceSysAttachment serviceSysAttachment;
		private VM_RelatedModule[] relatedModules;

		public MachiningController(
			IServiceMachining _serviceMachining,
			IServiceFinanceRecvDue _serviceFinanceRecvDue,
			IServiceSysEnumCode _serviceSysEnumCode,
			IServiceSysAttachment _serviceSysAttachment,
			ILogger<MachiningController> _logger
		)
			: base(_logger)
		{
			serviceMachining = _serviceMachining;
			serviceFinanceRecvDue = _serviceFinanceRecvDue;
			serviceSysEnumCode = _serviceSysEnumCode;
			serviceSysAttachment = _serviceSysAttachment;

			relatedModules = new VM_RelatedModule[]
			{
				new VM_RelatedModule()
				{
					id = "sys_attachment_shd",
					name = "送货单",
					count = 0,
				},
				new VM_RelatedModule()
				{
					id = "sys_attachment_zd",
					name = "当期账单",
					count = 0,
				},
				new VM_RelatedModule()
				{
					id = "finance_recv_due_list",
					name = "应收款",
					count = 0,
				},
			};
		}
		#endregion

		#region Utilities

		private VMS_BJG_Machining GetSearchModelFromJson(JObject paras)
		{
			var vm = new VMS_BJG_Machining
			{
				sno = TextHelper.GetString(paras, "sno"),
				machiningtype = TextHelper.GetInt(paras, "machiningtype"),
				partnername = TextHelper.GetString(paras, "partnername"),
				deliveryman = TextHelper.GetString(paras, "deliveryman"),
				date_s = TextHelper.GetDateTimeStart(paras, "date_s"),
				date_e = TextHelper.GetDateTimeEnd(paras, "date_e"),
				pnum = TextHelper.GetInt(paras, "pnum"),
				psize = TextHelper.GetInt(paras, "psize"),
				sortorder = TextHelper.GetString(paras, "sortorder"),
				ids = TextHelper.GetInts(paras, "ids"),
			};
			return vm;
		}

		/// <summary>
		/// 加工类型
		/// </summary>
		/// <param name="sessionCurrent"></param>
		/// <param name="selected"></param>
		/// <param name="defText"></param>
		/// <param name="defValue"></param>
		/// <returns></returns>
		private List<SelectListItem> GetMachiningTypeOptions(
			UserSession sessionCurrent,
			string defText = null,
			string defValue = "0"
		)
		{
			var list = serviceSysEnumCode.GetChildrenList("BJG_MachiningType", null, sessionCurrent, EnumCodeOptionValueType.Value);
			if (!string.IsNullOrEmpty(defText))
			{
				list.Insert(0, new SelectListItem(defText, defValue));
			}
			return list;
		}

		/// <summary>
		/// 收款类型
		/// </summary>
		/// <param name="defText"></param>
		/// <param name="defValue"></param>
		/// <returns></returns>
		private List<SelectListItem> GetRecvTypeOptions(
			string defText = null,
			string defValue = "0"
		)
		{
			List<SelectListItem> lst = new List<SelectListItem>()
			{
				new SelectListItem()
				{
					Value = ((int)FinancePayRecvType.记应收款).ToString(),
					Text = "记应收款",
				},
				new SelectListItem()
				{
					Value = ((int)FinancePayRecvType.现金收款).ToString(),
					Text = "现金收款",
				},
				new SelectListItem()
				{
					Value = ((int)FinancePayRecvType.预收款).ToString(),
					Text = "预收款",
				},
			};
			if (!string.IsNullOrEmpty(defText))
			{
				lst.Insert(0, new SelectListItem(defText, defValue));
			}
			return lst;
		}

		/// <summary>
		/// 收款方式
		/// </summary>
		/// <param name="defText"></param>
		/// <param name="defValue"></param>
		/// <returns></returns>
		private List<SelectListItem> GetRecvWayOptions(string defText = null, string defValue = "0")
		{
			List<SelectListItem> result = new List<SelectListItem>()
			{
				new SelectListItem()
				{
					Text = "现金",
					Value = ((int)FinancePayRecvWay.现金).ToString(),
				},
				new SelectListItem()
				{
					Text = "网银",
					Value = ((int)FinancePayRecvWay.网银).ToString(),
				},
				new SelectListItem()
				{
					Text = "微信",
					Value = ((int)FinancePayRecvWay.微信).ToString(),
				},
				new SelectListItem()
				{
					Text = "支付宝",
					Value = ((int)FinancePayRecvWay.支付宝).ToString(),
				},
				new SelectListItem()
				{
					Text = "其他",
					Value = ((int)FinancePayRecvWay.其他).ToString(),
				},
			};
			if (!string.IsNullOrEmpty(defText))
			{
				result.Insert(0, new SelectListItem(defText, defValue));
			}
			return result;
		}

		/// <summary>
		/// 收款状态
		/// </summary>
		/// <param name="defText"></param>
		/// <param name="defValue"></param>
		/// <returns></returns>
		private List<SelectListItem> GetRecvStateOptions(
			string defText = null,
			string defValue = "0"
		)
		{
			List<SelectListItem> result = new List<SelectListItem>()
			{
				new SelectListItem()
				{
					Text = "未收",
					Value = ((int)FinancePayRecvState.未收).ToString(),
				},
				new SelectListItem()
				{
					Text = "部分已收",
					Value = ((int)FinancePayRecvState.部分已收).ToString(),
				},
				new SelectListItem()
				{
					Text = "已收清",
					Value = ((int)FinancePayRecvState.已收清).ToString(),
				},
			};
			if (!string.IsNullOrEmpty(defText))
			{
				result.Insert(0, new SelectListItem(defText, defValue));
			}
			return result;
		}
		#endregion

		public IActionResult Index()
		{
			return RedirectToAction("List");
		}

		[ValidateSession]
		[HttpGet("list")]
		public IActionResult List()
		{
			ViewBag.ServiceCode = "F7826F3E57";
			UserSession sessionCurrent = GetSessionFromRequest();
			ViewBag.MachiningTypes = GetMachiningTypeOptions(sessionCurrent, "请选择", "0");
			ViewBag.RecvTypeOptions = GetRecvTypeOptions("请选择", "0");
			ViewBag.RecvWayOptions = GetRecvWayOptions("请选择", "0");
			ViewBag.RecvStateOptions = GetRecvStateOptions("请选择", "0");
			ViewBag.StateOptions = GetEnums(typeof(MachiningState));

			return View();
		}

		[ValidateSession]
		[HttpGet("mlist")]
		public IActionResult MobileList()
		{
			ViewBag.ServiceCode = "F7826F3E57";
			UserSession sessionCurrent = GetSessionFromRequest();
			ViewBag.MachiningTypes = GetMachiningTypeOptions(sessionCurrent, "请选择加工类型", "0");
			ViewBag.RecvTypeOptions = GetRecvTypeOptions("请选择", "0");
			ViewBag.RecvWayOptions = GetRecvWayOptions("请选择", "0");
			ViewBag.RecvStateOptions = GetRecvStateOptions("请选择", "0");
			ViewBag.StateOptions = GetEnums(typeof(MachiningState));

			return View();
		}

		[HttpPost("search")]
		public IActionResult Search([FromBody] JObject paras)
		{
			try
			{
				UserSession sessionCurrent = GetSessionFromRequest();
				var searchModel = GetSearchModelFromJson(paras);
				searchModel.orgids = new int[] { sessionCurrent.OrgID };
				PageData pageData = serviceMachining.GetPageList(searchModel, sessionCurrent);
				return new JsonResultPage(pageData);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(
					false,
					"请求出错",
					(int)HttpStatusCode.InternalServerError
				);
			}
		}

		[ValidateSession]
		[HttpGet("add")]
		public IActionResult Add()
		{
			ViewBag.ServiceCode = "F7826F3E57";
			UserSession sessionCurrent = GetSessionFromRequest();
			ViewBag.RelatedModules = relatedModules;
			ViewBag.MachiningTypes = GetMachiningTypeOptions(sessionCurrent, "请选择", "0");
			ViewBag.RecvTypeOptions = GetRecvTypeOptions("请选择", "0");
			ViewBag.RecvWayOptions = GetRecvWayOptions("请选择", "0");
			ViewBag.RecvStateOptions = GetRecvStateOptions("请选择", "0");
			ViewBag.StateOptions = GetEnums(typeof(MachiningState));
			try
			{
				BJG_Machining vm = new BJG_Machining()
				{
					DeliveryDate = DateTime.Today,
					RecvType = (int)FinancePayRecvType.记应收款,
					AddDate = DateTime.Now,
					AddManID = sessionCurrent.UserID,
					AddManName = sessionCurrent.UserName,
				};
				return View("Edit", vm);
			}
			catch (AuthException ex)
			{
				ViewBag.ErrorMsg = ex.Message;
				return View("Error401");
			}
			catch (Exception ex)
			{
				logger.LogError(ex, "打开新增页面出错");
				ViewBag.ErrorMsg =
					"打开新增页面失败，"
					+ (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
				return View("Error500");
			}
		}

		[ValidateSession]
		[HttpGet("madd")]
		public IActionResult MobileAdd()
		{
			ViewBag.ServiceCode = "F7826F3E57";
			UserSession sessionCurrent = GetSessionFromRequest();
			ViewBag.RelatedModules = relatedModules;
			ViewBag.MachiningTypes = GetMachiningTypeOptions(sessionCurrent);
			ViewBag.RecvTypeOptions = GetRecvTypeOptions();
			ViewBag.RecvWayOptions = GetRecvWayOptions();
			ViewBag.RecvStateOptions = GetRecvStateOptions();
			ViewBag.StateOptions = GetEnums(typeof(MachiningState));
			try
			{
				BJG_Machining vm = new BJG_Machining()
				{
					DeliveryDate = DateTime.Today,
					RecvType = (int)FinancePayRecvType.记应收款,
					AddDate = DateTime.Now,
					AddManID = sessionCurrent.UserID,
					AddManName = sessionCurrent.UserName,
				};
				return View("MobileEdit", vm);
			}
			catch (AuthException ex)
			{
				ViewBag.ErrorMsg = ex.Message;
				return View("Error401");
			}
			catch (Exception ex)
			{
				logger.LogError(ex, "打开新增页面出错");
				ViewBag.ErrorMsg =
					"打开新增页面失败，"
					+ (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
				return View("Error500");
			}
		}

		[ValidateSession]
		[HttpGet("edit/{id}")]
		[HttpGet("edit")]
		public IActionResult Edit(string id)
		{
			ViewBag.ServiceCode = "F7826F3E57";
			UserSession sessionCurrent = GetSessionFromRequest();
			ViewBag.RelatedModules = relatedModules;
			var types = GetMachiningTypeOptions(sessionCurrent, "请选择", "0");
			ViewBag.MachiningTypes = types;
			ViewBag.RecvTypeOptions = GetRecvTypeOptions("请选择", "0");
			ViewBag.RecvWayOptions = GetRecvWayOptions("请选择", "0");
			ViewBag.RecvStateOptions = GetRecvStateOptions("请选择", "0");
			ViewBag.StateOptions = GetEnums(typeof(MachiningState));
			try
			{
				int rowID = TextHelper.GetInt(id);
				var vm = serviceMachining.GetEditDetail(rowID, sessionCurrent);
				if (vm.MachiningType > 0)
				{
					var mType = types.FirstOrDefault(m => m.Value == vm.MachiningType.ToString());
					if (mType != null)
					{
						vm.MachiningTypeText = mType.Text;
					}
				}
				var statistics = serviceFinanceRecvDue.GetStatistics(vm.PartnerID);
				if (statistics != null)
				{
					vm.TotalRecvLeft = statistics.TotalRecvLeft;
				}
				//vm.SHDs = serviceSysAttachment.GetFiles(tableID: (int)TableCode.BJG_Machining, infoID: vm.ID).Select(x =>
				//{
				//	return new VM_File
				//	{
				//		ID = x.ID,
				//		name = x.FileName,
				//		url = x.FileUrl,
				//		size = x.FileSize
				//	};
				//}).ToList();
				//vm.DQZDs = serviceSysAttachment.GetFiles(tableID: (int)TableCode.BJG_Bill, infoID: vm.ID).Select(x =>
				//{
				//	return new VM_File
				//	{
				//		ID = x.ID,
				//		name = x.FileName,
				//		url = x.FileUrl,
				//		size = x.FileSize
				//	};
				//}).ToList();
				return View(vm);
			}
			catch (AuthException ex)
			{
				ViewBag.ErrorMsg = ex.Message;
				return View("Error401");
			}
			catch (Exception ex)
			{
				logger.LogError(ex, "打开编辑页面出错");
				ViewBag.ErrorMsg =
					"打开编辑页面失败，"
					+ (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
				return View("Error500");
			}
		}

		[ValidateSession]
		[HttpGet("medit/{id}")]
		[HttpGet("medit")]
		public IActionResult MobileEdit(string id)
		{
			ViewBag.ServiceCode = "F7826F3E57";
			UserSession sessionCurrent = GetSessionFromRequest();
			ViewBag.RelatedModules = relatedModules;
			var types = GetMachiningTypeOptions(sessionCurrent);
			ViewBag.MachiningTypes = types;
			ViewBag.RecvTypeOptions = GetRecvTypeOptions();
			ViewBag.RecvWayOptions = GetRecvWayOptions();
			ViewBag.RecvStateOptions = GetRecvStateOptions();
			ViewBag.StateOptions = GetEnums(typeof(MachiningState));
			try
			{
				int rowID = TextHelper.GetInt(id);
				var vm = serviceMachining.GetEditDetail(rowID, sessionCurrent);
				if (vm.MachiningType > 0)
				{
					var mType = types.FirstOrDefault(m => m.Value == vm.MachiningType.ToString());
					if (mType != null)
					{
						vm.MachiningTypeText = mType.Text;
					}
				}
				var statistics = serviceFinanceRecvDue.GetStatistics(vm.PartnerID);
				if (statistics != null)
				{
					vm.TotalRecvLeft = statistics.TotalRecvLeft;
				}
				vm.SHDs = serviceSysAttachment.GetFiles(tableID: (int)TableCode.BJG_Machining, infoID: vm.ID).Select(x =>
				{
					return new VM_File
					{
						ID = x.ID,
						name = x.FileName,
						url = x.FileUrl,
						size = x.FileSize
					};
				}).ToList();
				vm.DQZDs = serviceSysAttachment.GetFiles(tableID: (int)TableCode.BJG_Bill, infoID: vm.ID).Select(x =>
				{
					return new VM_File
					{
						ID = x.ID,
						name = x.FileName,
						url = x.FileUrl,
						size = x.FileSize
					};
				}).ToList();
				return View(vm);
			}
			catch (AuthException ex)
			{
				ViewBag.ErrorMsg = ex.Message;
				return View("Error401");
			}
			catch (Exception ex)
			{
				logger.LogError(ex, "打开编辑页面出错");
				ViewBag.ErrorMsg =
					"打开编辑页面失败，"
					+ (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
				return View("Error500");
			}
		}

		[ValidateSession]
		[HttpPost("save")]
		public IActionResult Save([FromBody] BJG_Machining vm)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();

				int rowID = serviceMachining.AddOrUpdate(vm, sessionCurrent);

				return new JsonResultBase(rowID);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(
					false,
					"新增失败",
					(int)HttpStatusCode.InternalServerError
				);
			}
		}

		/// <summary>
		/// 获取信息
		/// </summary>
		/// <returns></returns>
		[ValidateSession]
		[HttpGet("view/{id}")]
		[HttpGet("view")]
		public IActionResult ViewDetail(string id)
		{
			ViewBag.ServiceCode = "F7826F3E57";
			UserSession sessionCurrent = GetSessionFromRequest();
			ViewBag.RelatedModules = relatedModules;
			var types = GetMachiningTypeOptions(sessionCurrent);
			ViewBag.MachiningTypes = types;
			ViewBag.RecvTypeOptions = GetRecvTypeOptions();
			ViewBag.RecvWayOptions = GetRecvWayOptions();
			ViewBag.RecvStateOptions = GetRecvStateOptions();
			ViewBag.StateOptions = GetEnums(typeof(MachiningState));
			try
			{
				int rowID = TextHelper.GetInt(id);
				var vm = serviceMachining.GetViewDetail(rowID, sessionCurrent);
				if (vm.MachiningType > 0)
				{
					var mType = types.FirstOrDefault(m => m.Value == vm.MachiningType.ToString());
					if (mType != null)
					{
						vm.MachiningTypeText = mType.Text;
					}
				}
				vm.SHDs = serviceSysAttachment.GetFiles(tableID: (int)TableCode.BJG_Machining, infoID: vm.ID).Select(x =>
				{
					return new VM_File
					{
						ID = x.ID,
						name = x.FileName,
						url = x.FileUrl,
						size = x.FileSize
					};
				}).ToList();
				vm.DQZDs = serviceSysAttachment.GetFiles(tableID: (int)TableCode.BJG_Bill, infoID: vm.ID).Select(x =>
				{
					return new VM_File
					{
						ID = x.ID,
						name = x.FileName,
						url = x.FileUrl,
						size = x.FileSize
					};
				}).ToList();
				return View("View", vm);
			}
			catch (AuthException ex)
			{
				ViewBag.ErrorMsg = ex.Message;
				return View("Error401");
			}
			catch (EntityNotFoundException ex)
			{
				logger.LogError(ex, "打开查看页面出错");
				ViewBag.ErrorMsg =
					"未找到相关记录或记录已删除，详细错误信息如下："
					+ (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
				return View("Error404");
			}
			catch (Exception ex)
			{
				logger.LogError(ex, "打开查看页面出错");
				ViewBag.ErrorMsg =
					"打开查看页面失败，详细错误信息如下："
					+ (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
				return View("Error500");
			}
		}

		/// <summary>
		/// 获取信息
		/// </summary>
		/// <returns></returns>
		[ValidateSession]
		[HttpGet("mview/{id}")]
		[HttpGet("mview")]
		public IActionResult MobileViewDetail(string id)
		{
			ViewBag.ServiceCode = "F7826F3E57";
			UserSession sessionCurrent = GetSessionFromRequest();
			ViewBag.RelatedModules = relatedModules;
			var types = GetMachiningTypeOptions(sessionCurrent);
			ViewBag.MachiningTypes = types;
			ViewBag.RecvTypeOptions = GetRecvTypeOptions();
			ViewBag.RecvWayOptions = GetRecvWayOptions();
			ViewBag.RecvStateOptions = GetRecvStateOptions();
			ViewBag.StateOptions = GetEnums(typeof(MachiningState));
			try
			{
				int rowID = TextHelper.GetInt(id);
				var vm = serviceMachining.GetViewDetail(rowID, sessionCurrent);
				if (vm.MachiningType > 0)
				{
					var mType = types.FirstOrDefault(m => m.Value == vm.MachiningType.ToString());
					if (mType != null)
					{
						vm.MachiningTypeText = mType.Text;
					}
				}
				vm.SHDs = serviceSysAttachment.GetFiles(tableID: (int)TableCode.BJG_Machining, infoID: vm.ID).Select(x =>
				{
					return new VM_File
					{
						ID = x.ID,
						name = x.FileName,
						url = x.FileUrl,
						size = x.FileSize
					};
				}).ToList();
				vm.DQZDs = serviceSysAttachment.GetFiles(tableID: (int)TableCode.BJG_Bill, infoID: vm.ID).Select(x =>
				{
					return new VM_File
					{
						ID = x.ID,
						name = x.FileName,
						url = x.FileUrl,
						size = x.FileSize
					};
				}).ToList();
				return View("MobileView", vm);
			}
			catch (AuthException ex)
			{
				ViewBag.ErrorMsg = ex.Message;
				return View("Error401");
			}
			catch (EntityNotFoundException ex)
			{
				logger.LogError(ex, "打开查看页面出错");
				ViewBag.ErrorMsg =
					"未找到相关记录或记录已删除，详细错误信息如下："
					+ (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
				return View("Error404");
			}
			catch (Exception ex)
			{
				logger.LogError(ex, "打开查看页面出错");
				ViewBag.ErrorMsg =
					"打开查看页面失败，详细错误信息如下："
					+ (ex.InnerException != null ? ex.InnerException.Message : ex.Message);
				return View("Error500");
			}
		}

		/// <summary>
		/// 获取信息
		/// </summary>
		/// <returns></returns>
		[ValidateSession]
		[HttpPost("viewjson")]
		public IActionResult ViewJson([FromBody] JObject paras)
		{
			try
			{
				CheckParams();

				int id = TextHelper.GetInt(paras, "id");

				var model = serviceMachining.Get(id);

				if (model == null)
				{
					return new JsonResultBase(
						false,
						"未找到相关记录",
						(int)HttpStatusCode.NotFound
					);
				}

				return new JsonResultBase(model);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(
					false,
					"请求出错",
					(int)HttpStatusCode.InternalServerError
				);
			}
		}

		[ValidateSession]
		[HttpPost("delete")]
		public IActionResult Delete([FromBody] JObject paras)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();
				var rowID = TextHelper.GetInt(paras, "id");
				var result = serviceMachining.Delete(rowID, sessionCurrent);

				return new JsonResultBase(result);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(
					false,
					"删除失败",
					(int)HttpStatusCode.InternalServerError
				);
			}
		}

		[ValidateSession]
		[HttpPost("batchdelete")]
		public IActionResult BatchDelete([FromBody] int[] ids)
		{
			try
			{
				CheckParams();

				UserSession sessionCurrent = GetSessionFromRequest();

				var result = serviceMachining.BatchDelete(ids, sessionCurrent);

				return new JsonResultBase(result);
			}
			catch (AuthException ex)
			{
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.Unauthorized);
			}
			catch (BuzException ex)
			{
				return new JsonResultBase(false, ex.Message);
			}
			catch (ArgumentException ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(false, ex.Message, (int)HttpStatusCode.BadRequest);
			}
			catch (Exception ex)
			{
				logger.LogError(ex.InnerException ?? ex, ex.Message);
				return new JsonResultBase(
					false,
					"删除失败",
					(int)HttpStatusCode.InternalServerError
				);
			}
		}
	}
}
